package main

import "fmt"

func basicSearch(array []int, targetNumber int) int {
	if len(array) == 0 || array == nil {
		return -1
	}

	start := 0
	end := len(array) - 1
	middle := 0

	//写一个while循环
	for start+1 < end {
		middle = start + (end-start)/2
		if array[middle] == targetNumber {
			return middle
		} else if array[middle] > targetNumber {
			end = middle
		} else {
			start = middle
		}
	}

	if array[start] == targetNumber {
		return start
	}

	if array[end] == targetNumber {
		return end
	}

	return -1
}

func main() {
	//定义一个int类型的数组
	targetArray := []int{1, 4, 7, 9, 10, 14, 16, 20, 50, 76}

	result := basicSearch(targetArray, 10)

	fmt.Println(result)
}
